const session = require('express-session');
const MariaDBStore = require('express-session-mariadb-store');
var uid = require('uid-safe');


class SessionWrap{
    constructor(){
        let db = new MariaDBStore({
            host: process.env.NODE_DB_HOST ,
            user: process.env.NODE_DB_USER ,
            password: process.env.NODE_DB_PSD ,
            database: 'fregments' ,
            sessionTable: "session"
        });

        this.getDb = function(){
            return db;
        }
    }

    config( cookieVal ){
        let that = this;
        let sessionConfig = session({
            secret: 'fregment by Benjamin',
            resave: false,
            saveUninitialized: false,
            cookie: cookieVal,
            genid: function(req) {
                return that.genuuid() // use UUIDs for session IDs
            },
            store: this.dbStore()
        });

        return sessionConfig;
    }

    dbStore(){
        return this.getDb();
    }

    genuuid(){
        return uid.sync(18); //生成18位的id
    }
}

module.exports = SessionWrap;